package com.fenghongzhang.day010;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

public class MyTextView extends View {

    private String name;
    private float size;
    private int color;
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    public MyTextView(Context context) {
        super(context);
    }

    private void paserAttrs(Context context,AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);

         name = typedArray.getString(R.styleable.MyTextView_text);
         size = typedArray.getDimension(R.styleable.MyTextView_size,20);
         color = typedArray.getColor(R.styleable.MyTextView_color, Color.RED);
        typedArray.recycle();

    }



    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paserAttrs(context,attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        switch (mode){
            case MeasureSpec.EXACTLY:
                //match + 具体的值100;
                break;
            case MeasureSpec.AT_MOST:
                //wrap 走到这里
                break;
            case MeasureSpec.UNSPECIFIED:
                //任意大.很少用
                break;
        }
        setMeasuredDimension(100,200);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setTextSize(size);
        paint.setColor(color);
        canvas.drawText(name,0,40,paint);

    }
}
